Design Patterns
Intro
设计阶段,对一类问题进行通用设计,借用经典的设计
四个人提出的一个经典的设计模式:GOF
Design patterns have 4 essential elements:
Pattern name: increases vocabulary of designers
Problem: intent, context, when to apply
Solution: UML-like structure, abstract code
Consequences: results and tradeoffs
Three types of Gof patterns:
1.Creational patterns
deal with initialzing and configuring objects(对象的生成和初始化)
2.Structural patterns
类和对象的组成,侧重于静态结构的设计,接口和实现
3.Behavioral patterns
解决对象之间动态的交互
Structural patterns
Assemble objects to realize new functionality
例子:
Adapter:Converts interface of a class into one that clients expect
Bridge:Links abstraction with many possible implementations
Composite:Represents part-whole hierarchies as tree structures
Decorator:Attach additional responsibilities to object dynamically
Facade:Simplifies the interface for a subsystem
Flyweight:Shares many fine-grained objects efficiently
Proxy:
proxy
Problem:目标对象不能直接被调用
Solution:proxy作为一个快捷的代理去访问
Proxy有三类:
1.Remote proxy:
2.virtual proxy:执行效率问题,需要实际使用再装载
3.protected proxy:受保护的
内容: 提供一个surrogate 或这 placeholder for another object to control access to it
Adapter pattern
Problem:接口不匹配
Solution:(接口和实现分离)Convert original interface component into another one through an intermediate adapter.
Benefits of Adapter pattern:
1.Reduces coupling to implementation specific details
2.多态性和间接性揭示了所提供的基本行为
3.Including name of design pattern in new class (e.g., TaxMasterAdapter) in class diagrams and code communicates to other developers in terms of known design patterns
区别:一个接口不行,一个访问不行
1.代理(Proxy)模式给某一个对象提供一个代理,并由代理对象控制对原对象的引用。
2.适配器模式(Adapter)把一个类的接口变换成客户端所期待的另一种接口,从而使原本接口不匹配而无法在一起工作的两个类能够在一起工作。
3. Proxy的关注点是职能转移,引入代理层代替目标端与调用端进行沟通,而且代理层和目标端具有相同的服务结构(继承同一个接口)。
Adapter的关注点是接口变换,引入一个符合调用端要求的“转化器”实现目标端与调用端的沟通,而且转化器和目标端的服务结构式是不一样的。
Composite
Composite lets clients treat individual objects and compositions of objects uniformly.
功能:是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构
Facade Pattern
Def:外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。
客户端不需要知道系统内部的复杂联系,整个系统只需提供一个”接待员”即可
Creational Patterns
以下三大特性能够帮助我们更好的组织类的关系:封装、继承、多态
以下三大内容能够帮助我们更好的设计类中方法如何运行:顺序、判断、循环
例子:
Singleton: Guarantee access to a singular (sole) instance
Simple Factory: Create specialized, complex objects
Abstract Factory: Create a family of specialized factories
Factory Method: Define an interface for creating an object, but let subclasses decide which class to instantiate
Builder: Construct a complex object step by step
Prototype: Clone new instances from a prototype
Lazy initialization: Delay costly creation until it is needed
Singleton pattern
Def:A class with just instance and provide a global point of access
它提供了一种创建对象的最佳方式。这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。
Simple Factory pattern
Def:Who should be responsible for creating objects when there are special considerations, such as complex logic, a desire to separate the creation responsibilities for better cohesion, and so forth
工厂模式提供了一种将对象的实例化过程封装在工厂类中的方式。通过使用工厂模式,可以将对象的创建与使用代码分离,提供一种统一的接口来创建不同类型的对象。
Example:您需要一辆汽车,可以直接从工厂里面提货,而不用去管这辆汽车是怎么做出来的,以及这个汽车里面的具体实现
Abstract FActory Pattern
抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式
接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
example:应用实例:工作了,为了参加一些聚会,肯定有两套或多套衣服吧,比如说有商务装(成套,一系列具体产品)、时尚装(成套,一系列具体产品),甚至对于一个家庭来说,可能有商务女装、商务男装、时尚女装、时尚男装,这些也都是成套的,即一系列具体产品。假设一种情况(现实中是不存在的,但有利于说明抽象工厂模式),在您的家中,某一个衣柜(具体工厂)只能存放某一种这样的衣服(成套,一系列具体产品),每次拿这种成套的衣服时也自然要从这个衣柜中取出了
Advantage:
1.Separates responsibility of complex creation into cohesive helper classes
2.Hides complex creation logic, such as initialization from a file
3.Handles memory management strategies, such or recycling or caching
Behavioral Patterns
对象之间并不是孤立的,一个对象的运行会影响到其他对象。行为型模式通过划分类和对象的职责,关注系统在运行时对象之间的交互关系。
行为型模式分为类行为型模式和对象行为型模式两种:
1.类行为型模式:类的行为型模式使用继承关系在几个类之间分配行为,类行为型模式主要通过多态等方式来分配父类与子类的职责。
2.对象行为型模式:对象的行为型模式则使用对象的聚合关联关系来分配行为,对象行为型模式主要是通过对象关联等方式来分配两个或多个类的职责
例子:
Chain of Responsibility:Request delegated to the responsible service provider
Command:Request or Action is first-class object, hence storable
Iterator:Aggregate and access elements sequentially
Interpreter:Language interpreter for a small grammar
Mediator:Coordinates interactions between its associates
Memento:Snapshot captures and restores object states privately
Observer:Observers update automatically when observed object changes
State:Object whose behavior depends on its state
Strategy:Abstraction for selecting one of many algorithms
Template Method:Algorithm with some steps supplied by derived class
Visitor:Operations applied to elements of a heterogeneous object structure
Observer pattern
Def:当一个对象的状态发生改变时,其所有依赖者都会收到通知并自动更新。
当对象间存在一对多关系时,则使用观察者模式(Observer Pattern)。比如,当一个对象被修改时,则会自动通知依赖它的对象。观察者模式属于行为型模式。
Used in Model-View-Controller framework(Rails的结构)
1.Model is problem domain
2.View is windowing system
3.Controller is mouse/keyboard control
Command pattern
Def:是一种数据驱动的设计模式,它属于行为型模式。请求以命令的形式包裹在对象中,并传给调用对象。调用对象寻找可以处理该命令的合适的对象,并把该命令传给相应的对象,该对象执行命令。可以用不同的需求参数化命令
定义三个角色:1、received 真正的命令执行对象 2、Command 3、invoker 使用命令对象的入口
调用请求描述为command的一个对象,不同的请求实现不同的concretecommand
何时使用:在某些场合,比如要对行为进行”记录、撤销/重做、事务”等处理,这种无法抵御变化的紧耦合是不合适的。在这种情况下,如何将”行为请求者”与”行为实现者”解耦?将一组行为抽象为对象,可以实现二者之间的松耦合。
结果:
您可以撤消/重做任何命令
每个命令存储恢复状态所需的内容
可以将命令存储在堆栈或队列中
命令处理器模式维护历史
添加新命令很容易,因为不必更改现有类
命令是一个抽象类,从中派生新类
execute0、undo()和redoO)是多态的函数
State Pattern – Motivation
Problem:当一个对象接收到一个请求时候,可能实行不同的操作,因为对象处于不同的状态
意图:允许对象在内部状态发生改变时改变它的行为,对象看起来好像修改了它的类。
Builder Pattern
意图:Separate the construction of a complex object from its representation so that the same construction process can create different representations.
建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象
将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示
一种角色director,一种是builder
Prototype Pattern
Problem:原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式之一。
Intend:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
Bridge pattern
Problem:在有多种可能会变化的情况下,用继承会造成类爆炸问题,扩展起来不灵活。
意图:将抽象部分与实现部分分离,使它们都可以独立的变化。
Decorator Pattern
装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。
意图:动态地给一个对象添加一些额外的职责。就增加功能来说,装饰器模式相比生成子类更为灵活。
主要解决:一般的,我们为了扩展一个类经常使用继承方式实现,由于继承为类引入静态特征,并且随着扩展功能的增多,子类会很膨胀。
Intent
Attach additional responsibilities to an object dynamically.
Decorators provide a flexible alternative to subclassing for extending functionality.
优点:装饰类和被装饰类可以独立发展,不会相互耦合,装饰模式是继承的一个替代模式,装饰模式可以动态扩展一个实现类的功能。
Strategy Pattern
Intent
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.
在策略模式(Strategy Pattern)中一个类的行为或其算法可以在运行时更改
意图:定义一系列的算法,把它们一个个封装起来, 并且使它们可相互替换。
主要解决:在有多种算法相似的情况下,使用 if…else 所带来的复杂和难以维护。
优点: 1、算法可以自由切换。 2、避免使用多重条件判断。 3、扩展性良好。
Iterator Pattern
意图:提供一种方法顺序访问一个聚合对象中各个元素, 而又无须暴露该对象的内部表示。
主要解决:不同的方式来遍历整个整合对象。
优点: 1、它支持以不同的方式遍历一个聚合对象。 2、迭代器简化了聚合类。 3、在同一个聚合上可以有多个遍历。 4、在迭代器模式中,增加新的聚合类和迭代器类都很方便,无须修改原有代码。
总结
设计模式的优点:
Design patterns enable large-scale reuse of software architectures and also help document systems
Patterns explicitly capture expert knowledge and design tradeoffs and make it more widely available
Patterns help improve developer communication
Pattern names form a common vocabulary
- Title: Design Patterns
- Author: 杨小鹤
- Created at: 2024-02-01 14:26:08
- Updated at: 2023-11-03 15:28:20
- Link: https://redefine.ohevan.com/2024/02/01/SW/sw9/
- License: This work is licensed under CC BY-NC-SA 4.0.